package com.example.rotateimageview.marquee;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.widget.TextView;

import com.example.rotateimageview.R;

/**
 *
 */
public class MarqueeTextView extends TextView {
    private Context mContext;
    private float textLength = 0f;//文本长度
    private float viewWidth = 0f; //显示的长度
    private float step = 0f;//文字的横坐标
    private float step2 = 0f;
    private float y = 0f;//文字的纵坐标
    private float temp_view_plus_two_text_length = 0.0f;//用于计算的临时变量
    //    private float  nextSpace; //文字之间间隔10dp
    public boolean isStarting = false;//是否开始滚动
    private Paint paint = new Paint();//绘图样式
    private String text = "";//文本内容
    private float speed = 1.5f;// 滚动速度
    private float stepWidth = 40f;
    private boolean isLongTxt;


    public MarqueeTextView(Context context) {
        super(context);
        mContext = context;
    }
    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init(attrs);
    }
    public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        init(attrs);
    }

    public void setScrollText(String scroolText){
        if(scroolText == null){
            scroolText = "";
        }
        if(scroolText.equals(text)){
            return;
        }
        stopScroll();
        step = 0;
        text = scroolText;
        measureLength();
        startScroll();
        isPaint2 = false;
    }

    public void init(AttributeSet attrs){
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.MarqueeTextView);
        speed = typedArray.getFloat(R.styleable.MarqueeTextView_speed,1.5f);
        typedArray.recycle();
        paint.setColor(getCurrentTextColor());//设置文字颜色
        paint.setTextSize(getTextSize());//设置字体大小
        paint.setAntiAlias(true);
//        paint.setColor(Color.parseColor("#ffffff"));//设置文字颜色
//        paint.setTextSize(DensityUtil.dip2px(mContext, 12f));//设置字体大小
//        lableSpace=DensityUtil.dip2px(mContext,15f);
//        viewWidth = DensityUtil.dip2px(mContext,viewWidth);
        text = getText().toString();
        measureLength();
//        measureLength();
//        y = getTextSize() + getPaddingTop();
        startScroll();
    }

    private void measureLength(){
        viewWidth = getMeasuredWidth();
        textLength = paint.measureText(text);
        step = textLength;
        temp_view_plus_two_text_length = viewWidth + textLength;
        if(textLength>viewWidth){
            isLongTxt = true;
        }else{
            isLongTxt = textLength + stepWidth >= viewWidth;
        }
        y = getTextSize() + getPaddingTop();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        viewWidth = DensityUtil.dip2px(mContext,viewWidth);
        viewWidth = MeasureSpec.getSize(widthMeasureSpec);
        temp_view_plus_two_text_length = viewWidth + textLength;
        if(textLength>viewWidth){
            isLongTxt = true;
        }else{
            isLongTxt = textLength + stepWidth >= viewWidth;
        }
    }

    @Override
    public Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        SavedState ss = new SavedState(superState);
        ss.step = step;
        ss.isStarting = isStarting;
        return ss;
    }

    @Override
    public void onRestoreInstanceState(Parcelable state) {
        if (!(state instanceof SavedState)) {
            super.onRestoreInstanceState(state);
            return;
        }
        SavedState ss = (SavedState)state;
        super.onRestoreInstanceState(ss.getSuperState());
        step = ss.step;
        isStarting = ss.isStarting;
    }

    public static class SavedState extends BaseSavedState {
        public boolean isStarting = false;
        public float step = 0.0f;
        SavedState(Parcelable superState) {
            super(superState);
        }
        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeBooleanArray(new boolean[]{isStarting});
            out.writeFloat(step);
        }

        public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
            @Override
            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }
        };
        private SavedState(Parcel in) {
            super(in);
            boolean[] b = new boolean[]{isStarting};
            in.readBooleanArray(b);
            if(b != null && b.length > 0)
                isStarting = b[0];
            step = in.readFloat();
        }
    }

    public void startScroll() {
        isStarting = true;
        invalidate();
    }


    public void stopScroll() {
        isStarting = false;
        invalidate();
    }
    private boolean isPaint2 = false;

    @Override
    public void onDraw(Canvas canvas) {
        //音乐图标
        canvas.drawText(text, viewWidth - step, y, paint);
        if(isPaint2){
            canvas.drawText(text, viewWidth - step2, y, paint);
        }

        if(!isStarting){
            return;
        }
        step += speed;//越小越慢
        if(step > temp_view_plus_two_text_length) {//如果文字全部不可见
            step = 0;
            step = step2;
            isPaint2 = false;
        }
        if(isPaint2) {
            step2 += speed;
        }else {
            if(isLongTxt){
                if(step>= textLength+stepWidth){
                    isPaint2 = true;
                    step2 = 0;
                }
            }else {
                if (step >= viewWidth ) {
                    isPaint2 = true;
                    step2 = 0;
                }
            }
        }
        invalidate();
    }
}